package com.zkdcloud.controller.mobile;

import com.xdja.interceptor.annotation.BeforeProcess;
import com.xdja.interceptor.bean.ResultBean;
import com.zkdcloud.aspectInterceptor.advice.MobilePageAdvice;
import com.zkdcloud.aspectInterceptor.advice.MobileTokenAdvice;
import com.zkdcloud.domain.Business;
import com.zkdcloud.domain.Category;
import com.zkdcloud.domain.Product;
import com.zkdcloud.domain.User;
import com.zkdcloud.dto.FavoriteDisplay;
import com.zkdcloud.dto.TerminalResultsPage;
import com.zkdcloud.service.BusinessService;
import com.zkdcloud.service.CategoryService;
import com.zkdcloud.service.ProductService;
import org.jfaster.mango.plugin.page.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

import static com.zkdcloud.constant.Constants.HTTP_CONSTANTS.REQUEST_USER;

/**
 * 终端buiness
 *
 * @author zk123
 * @version 2018/5/15
 */
@RestController
@RequestMapping(value = "/api")
public class MobileBusinessController {
    /**
     * business service
     */
    @Autowired
    private BusinessService businessService;
    /**
     * 产品service
     */
    @Autowired
    private ProductService productService;
    /**
     * 分类service
     */
    @Autowired
    private CategoryService categoryService;
    /**
     * 获取商家列表
     *
     * @param page page
     * @return rb
     */
    @BeforeProcess(advice = {MobileTokenAdvice.class,MobilePageAdvice.class})
    @RequestMapping(value = "/businesses",method = RequestMethod.GET)
    public ResultBean getBusinessList(Page page,Map<String,Object> map){
        User user = (User) map.get(REQUEST_USER);
        List<Business> businessList = businessService.getAll(user.getId(),page);
        Integer pageCount = page.getTotal() % page.getPageSize() == 0 ? page.getTotal() / page.getPageSize() : page.getTotal() / page.getPageSize() + 1;
        TerminalResultsPage<Business> resultBean = new TerminalResultsPage(page.getTotal(),pageCount,page.getPageNum(),page.getPageSize());
        resultBean.setItems(businessList);
        return resultBean;
    }

    @RequestMapping(value = "/businesses/{bid}/products",method = RequestMethod.GET)
    public Object products(@PathVariable String bid){
        List<Category> categoryList = categoryService.getAllByBid(bid);
        for (Category category : categoryList) {
            List<Product> productList = productService.getAvaliByCid(category.getId(),null);
            category.setProducts(productList);
        }
        return categoryList;
    }

    /**
     * 添加喜欢
     *
     * @param bid bid
     * @return return
     */
    @BeforeProcess(advice = MobileTokenAdvice.class)
    @RequestMapping(value = "/businesses/{bid}/favorite",method = RequestMethod.POST)
    public Object addFavorite(@PathVariable String bid, Map<String,Object> map){
        User user = (User) map.get(REQUEST_USER);
        businessService.favoriteBusiness(user.getId(),bid);
        return new Object();
    }
}
